package it.cqupt.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import it.cqupt.dao.system.ModuleDao;
import it.cqupt.dao.system.UserDao;
import it.cqupt.domain.system.Module;
import it.cqupt.domain.system.User;
import it.cqupt.factory.MapperFactory;
import it.cqupt.service.system.ModuleService;
import it.cqupt.service.system.UserService;
import it.cqupt.utils.MD5Util;
import it.cqupt.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {
    SqlSession sqlSession = null;
    @Override
    public void save(User user) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession, UserDao.class);
            String id = UUID.randomUUID().toString();
            user.setId(id);
            String s = MD5Util.MD5Encode(user.getPassword(),"utf-8");
            user.setPassword(s);
            mapper.save(user);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delete(User user) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession,UserDao.class);
            mapper.delete(user);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(User user) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession,UserDao.class);
            mapper.update(user);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public User findById(String id) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession,UserDao.class);
            return mapper.findById(id);
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<User> findAll() {
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession,UserDao.class);
            return mapper.findAll();
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public PageInfo findAll(int page, int size) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession,UserDao.class);
            PageHelper.startPage(page,size);
            List<User> list = mapper.findAll();
            PageInfo pageInfo = new PageInfo(list);
            return pageInfo;
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void updateRole(String userId, String[] roleIds) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession,UserDao.class);
            mapper.deleteUserRole(userId);
            for (String roleId : roleIds) {
                mapper.insertUserRole(userId,roleId);
            }
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public User login(String email, String password) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserDao mapper = MapperFactory.getMapper(sqlSession,UserDao.class);
            String new_password = MD5Util.MD5Encode(password,"utf-8");
//            System.out.println(new_password);
            return mapper.findByEmailAndPwd(email,new_password);
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Module> findModuleListByUid(String id) {
        try {
            sqlSession = MapperFactory.getSqlSession();
            ModuleDao mapper = MapperFactory.getMapper(sqlSession,ModuleDao.class);
            return mapper.findModuleListByUid(id);
        }catch (Exception e){
            throw new RuntimeException(e);
            //记录日志
        }finally {
            try {
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
